package com.dingup.paike.controller.message;

import com.dingup.api.base.HttpResContent;
import com.dingup.paike.adapter.pk.PkLessonUserAdapter;
import com.dingup.paike.adapter.pk.message.RemindOfClassForUser;
import com.dingup.paike.bean.aobean.MasterSignInAO;
import com.dingup.paike.dao.PkLessonInfoMapper;
import com.dingup.paike.enums.PkLessonUserTypeEnum;
import com.dingup.paike.model.pk.PkLessonInfo;
import com.dingup.paike.service.message.UserRemindService;
import com.suyeer.basic.util.BHttpResUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import static com.dingup.api.base.BaseUtil.saveErrorMsgAndData;
import static com.dingup.api.base.BaseUtil.saveSuccessMsgAndData;

/**
 * Created by wancheng on 2018/10/18.
 */
@RestController
@RequestMapping("/message")
public class MessageController {

    private static Logger logger = LoggerFactory.getLogger(MessageController.class);

    @Autowired
    UserRemindService userRemindService;
    @Autowired
    PkLessonInfoMapper pkLessonInfoMapper;

    @RequestMapping(value = "/get_lesson_remind_for_teacher.do", method = RequestMethod.POST)
    public void getLessonRemindForTeacher(HttpServletRequest request,
                                          HttpServletResponse response,
                                          String teacherId,
                                          String pkMessageDetailsId) {
        HttpResContent<RemindOfClassForUser> hrc = new HttpResContent<>(request, response);
        hrc.setFuncDes("老师获取明天上课提醒");
        try {
            RemindOfClassForUser remind = userRemindService.getRemindOfClassForUser(teacherId, PkLessonUserTypeEnum.TEACHER, pkMessageDetailsId);
            saveSuccessMsgAndData(hrc, remind);
        } catch (Exception e) {
            saveErrorMsgAndData(hrc, e);
        } finally {
            BHttpResUtil.writeResult(hrc, true);
        }
    }

    @RequestMapping(value = "/get_lesson_remind_for_student.do", method = RequestMethod.POST)
    public void getLessonRemindForStudent(HttpServletRequest request,
                                          HttpServletResponse response,
                                          String studentId,
                                          String pkMessageDetailsId) {
        HttpResContent<RemindOfClassForUser> hrc = new HttpResContent<>(request, response);
        hrc.setFuncDes("学生获取明天上课提醒");
        try {
            RemindOfClassForUser remind = userRemindService.getRemindOfClassForUser(studentId, PkLessonUserTypeEnum.STUDENT, pkMessageDetailsId);
            saveSuccessMsgAndData(hrc, remind);
        } catch (Exception e) {
            saveErrorMsgAndData(hrc, e);
        } finally {
            BHttpResUtil.writeResult(hrc, true);
        }
    }

    @RequestMapping(value = "/get_lesson_remind_for_master.do", method = RequestMethod.POST)
    public void getLessonRemindForMaster(HttpServletRequest request,
                                         HttpServletResponse response,
                                         String masterId,
                                         String pkMessageDetailsId) {
        HttpResContent<RemindOfClassForUser> hrc = new HttpResContent<>(request, response);
        hrc.setFuncDes("班主任获取明天上课提醒");
        try {
            RemindOfClassForUser remind = userRemindService.getRemindOfClassForUser(masterId, PkLessonUserTypeEnum.MASTER, pkMessageDetailsId);
            saveSuccessMsgAndData(hrc, remind);
        } catch (Exception e) {
            saveErrorMsgAndData(hrc, e);
        } finally {
            BHttpResUtil.writeResult(hrc, true);
        }
    }

    @RequestMapping(value = "/get_lesson_remind_about_to_begin.do", method = RequestMethod.POST)
    public void getLessonRemindAboutToBegin(HttpServletRequest request,
                                            HttpServletResponse response,
                                            Integer pkLessonId) {
        HttpResContent<PkLessonUserAdapter> hrc = new HttpResContent<>(request, response);
        hrc.setFuncDes("获取即将上课提醒");
        try {
            PkLessonUserAdapter pkLessonUserAdapter = userRemindService.getLessonRemindAboutToBegin(pkLessonId);
            saveSuccessMsgAndData(hrc, pkLessonUserAdapter);
        } catch (Exception e) {
            saveErrorMsgAndData(hrc, e);
        } finally {
            BHttpResUtil.writeResult(hrc, true);
        }
    }

    @RequestMapping(value = "/get_lesson_confirm_details_for_class.do", method = RequestMethod.POST)
    public void getLessonConfirmDetailsForClass(HttpServletRequest request,
                                                HttpServletResponse response,
                                                Integer pkLessonId,
                                                String pkMessageDetailsId) {
        HttpResContent<MasterSignInAO> hrc = new HttpResContent<>(request, response);
        hrc.setFuncDes("查看上课提醒的确认情况");
        try {
            MasterSignInAO masterSignInAO = userRemindService.getLessonConfirmDetailsForClass(pkLessonId, pkMessageDetailsId);
            saveSuccessMsgAndData(hrc, masterSignInAO);
        } catch (Exception e) {
            saveErrorMsgAndData(hrc, e);
        } finally {
            BHttpResUtil.writeResult(hrc, true);
        }
    }

    @RequestMapping(value = "/get_lesson_sign_in_for_master.do", method = RequestMethod.POST)
    public void getLessonSignInForMaster(HttpServletRequest request,
                                         HttpServletResponse response,
                                         String masterId,
                                         String pkMessageDetailsId) {
        HttpResContent<RemindOfClassForUser> hrc = new HttpResContent<>(request, response);
        hrc.setFuncDes("签到表");
        try {
            RemindOfClassForUser remind = userRemindService.getPkLessonSignIn(masterId, pkMessageDetailsId);
            saveSuccessMsgAndData(hrc, remind);
        } catch (Exception e) {
            saveErrorMsgAndData(hrc, e);
        } finally {
            BHttpResUtil.writeResult(hrc, true);
        }
    }

    @RequestMapping(value = "/get_lesson_sign_in_details.do", method = RequestMethod.POST)
    public void getLessonSignInForMaster(HttpServletRequest request,
                                         HttpServletResponse response,
                                         Integer pkLessonId) {
        HttpResContent<MasterSignInAO> hrc = new HttpResContent<>(request, response);
        hrc.setFuncDes("签到详情");
        try {
            MasterSignInAO signInAO = userRemindService.getPkLessonSignInDetails(pkLessonId);
            saveSuccessMsgAndData(hrc, signInAO);
        } catch (Exception e) {
            saveErrorMsgAndData(hrc, e);
        } finally {
            BHttpResUtil.writeResult(hrc, true);
        }
    }

    @RequestMapping(value = "/student_sign_in.do", method = RequestMethod.POST)
    public void studentSignIn(HttpServletRequest request,
                              HttpServletResponse response,
                              Integer pkLessonId,
                              String studentId) {
        HttpResContent<Boolean> hrc = new HttpResContent<>(request, response);
        hrc.setFuncDes("签到");
        try {
            userRemindService.studentSignIn(pkLessonId, studentId);
            saveSuccessMsgAndData(hrc, true);
        } catch (Exception e) {
            saveErrorMsgAndData(hrc, e);
        } finally {
            BHttpResUtil.writeResult(hrc, true);
        }
    }

    @RequestMapping(value = "/message_confirm.do", method = RequestMethod.POST)
    public void messageConfirm(HttpServletRequest request,
                               HttpServletResponse response,
                               String pkMessageDetailsId) {
        HttpResContent<Boolean> hrc = new HttpResContent<>(request, response);
        hrc.setFuncDes("消息确定");
        try {
            userRemindService.messageConfirm(pkMessageDetailsId);
            saveSuccessMsgAndData(hrc, true);
        } catch (Exception e) {
            saveErrorMsgAndData(hrc, e);
        } finally {
            BHttpResUtil.writeResult(hrc, true);
        }
    }

    @RequestMapping(value = "/lesson_class_hour_details.do", method = RequestMethod.POST)
    public void getPkLessonClassHourConfirmDetails(HttpServletRequest request,
                                                   HttpServletResponse response,
                                                   Integer pkLessonId) {
        HttpResContent<MasterSignInAO> hrc = new HttpResContent<>(request, response);
        hrc.setFuncDes("确认课时--详情");
        try {
            MasterSignInAO signInAO = userRemindService.getPkLessonClassHourConfirmDetails(pkLessonId);
            saveSuccessMsgAndData(hrc, signInAO);
        } catch (Exception e) {
            saveErrorMsgAndData(hrc, e);
        } finally {
            BHttpResUtil.writeResult(hrc, true);
        }
    }

    @RequestMapping(value = "/lesson_class_hour_confirm.do", method = RequestMethod.POST)
    public void lessonClassHourConfirm(HttpServletRequest request,
                                       HttpServletResponse response,
                                       Integer pkLessonId,
                                       Boolean ifClassFee,
                                       Float classHour) {
        HttpResContent<Boolean> hrc = new HttpResContent<>(request, response);
        hrc.setFuncDes("确认课时");
        try {
            PkLessonInfo current = pkLessonInfoMapper.selectByPrimaryKey(pkLessonId);
            if (current == null) {
                throw new Exception("课节不存在");
            }
            if (current.getIfLessonFinish()) {
                throw new Exception("该课节已经确认过课时");
            }
            userRemindService.lessonClassHourConfirm(pkLessonId, ifClassFee, classHour);
            saveSuccessMsgAndData(hrc, true);
        } catch (Exception e) {
            saveErrorMsgAndData(hrc, e);
        } finally {
            BHttpResUtil.writeResult(hrc, true);
        }
    }

}
